#include "registerwidget.h"
#include "ui_registerwidget.h"
#include <QMouseEvent>
#include <QMessageBox>
#include "QNetworkAccessManager"
#include "QNetworkRequest"
#include "QNetworkReply"
#include "QJsonParseError"
#include "QDebug"

registerWidget::registerWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::registerWidget)
{
    ui->setupUi(this);

    basicSettings();
    connectSettings();
}

registerWidget::~registerWidget()
{
    delete ui;
}

void registerWidget::basicSettings()
{
    ui->labPixmap->setScaledContents(true);
    ui->labPixmap->setPixmap(QPixmap(":/picture/register2.png"));
    //消除边框
    this->setWindowFlag(Qt::FramelessWindowHint);
    //密码框
    ui->editPassWord->setEchoMode(QLineEdit::Password);
    ui->editPWAgain->setEchoMode(QLineEdit::Password);

    ui->editId->setMaxLength(8);
    ui->editName->setMaxLength(7);
    ui->editPassWord->setMaxLength(8);
    ui->editPWAgain->setMaxLength(8);
}

void registerWidget::connectSettings()
{
    //关闭与最小化
    connect(ui->btnClose, &QPushButton::clicked, this, &QWidget::close);
    connect(ui->btnMinimize, &QPushButton::clicked, this, &QWidget::showMinimized);

    //注册与取消
    connect(ui->btnRegister, &QPushButton::clicked, this, &registerWidget::btnRegister_clicked);
    connect(ui->btnCancel, &QPushButton::clicked, this, &registerWidget::btnCancel_clicked);
}

void registerWidget::emptyWidget()
{
    ui->editId->setText("");
    ui->editName->setText("");
    ui->editPassWord->setText("");
    ui->editPWAgain->setText("");
}

void registerWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->y() < ui->menuBar->height() && event->buttons() & Qt::LeftButton)
    {
        address = event->globalPos();
    }
}

void registerWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(event->y() < ui->menuBar->height() && event->buttons() & Qt::LeftButton)
    {
        int dx = event->globalX() - address.x();
        int dy = event->globalY() - address.y();
        this->move(this->x()+dx, this->y()+dy);
        address = event->globalPos();
    }
}

void registerWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->y() < ui->menuBar->height() && event->buttons() & Qt::LeftButton)
    {
        int dx = event->globalX() - address.x();
        int dy = event->globalY() - address.y();
        this->move(this->x()+dx, this->y()+dy);
        address = event->globalPos();
    }
}

void registerWidget::dealMsg(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QString s = array;
    if(QString::compare(s,"注册成功") == 0)
    {
        QMessageBox::information(this, "账号注册", "账号注册成功！", QMessageBox::Ok, QMessageBox::Ok);
        btnCancel_clicked();
    }
    else
    {
        QMessageBox::critical(this, "错误", "该账号已存在！", QMessageBox::Ok, QMessageBox::Ok);
    }
}

void registerWidget::btnRegister_clicked()
{
    if(QString::compare(ui->editPassWord->text(), ui->editPWAgain->text()) == 0)
    {
        // URL
        QString baseUrl = "http://127.0.0.1:5000//Register?Userid=" + ui->editId->text() + "&Password=" + ui->editPassWord->text() + "&Nickname=" + ui->editName->text();
        QUrl url(baseUrl);

        // 构造请求QNetworkRequest
        QNetworkRequest request;
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
        request.setUrl(url);

        QNetworkAccessManager *manager = new QNetworkAccessManager();

        // 发送get请求
        manager->get(request);

        connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(dealMsg(QNetworkReply*)));
    }
    else
    {
        QMessageBox::critical(this, "错误", "输入密码不一致！", QMessageBox::Ok, QMessageBox::Ok);
    }
}

void registerWidget::btnCancel_clicked()
{
    this->close();
    emptyWidget();
}
